'''
题：编写一个程序来计算输入中单词的频率。 按字母顺序对键进行排序后输出。
假设为程序提供了以下输入：
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
然后，输出应该是：
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
'''
#方法1
#这应该不难吧   将输入通过空格分割之后  进行相应的操作count,sorted
# dac = {}
# DC = input("输入单词:").split(" ")
# for i in DC:
#     dac[i] = DC.count(i)
# print(dac)
# dac = {'New': 1, 'to': 1, 'Python': 5, 'or': 2, 'chossing': 1,
#        'between': 1, '2': 2, 'and': 1, '3?': 1, 'Read': 1, '3.': 1}
# #对字典是使用sorted时传入的是字典的key，按照字典进行排序
# sortdac = sorted(dac,key=lambda x:x)
# for i in sortdac:
#     print("%s:%d"%(i,dac[i]))

#方法2:使用的是字典的get方法来实现统计的
freq = {}  # frequency of words in text
line = ['New', 'to' , 'Python','Python', 'or', 'chossing',
       'between', '2', 'and', '3?', 'Read', '3.']
for word in line:
    freq[word] = freq.get(word, 0) + 1
words = sorted(freq.keys())  # 按key值对字典排序

for w in words:
    print("%s:%d" % (w, freq[w]))
